package
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.FileListEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.FileEvent;
	import mx.managers.PopUpManager;
	
	import pure.engine.Despair;
	import pure.engine.system.SystemManager;
	
	import spark.components.Button;
	
	import windows.SwfSectionLoadWindow;
	
	
	public class ControlManager
	{
		
		public static var eventDispatcher:EventDispatcher=new EventDispatcher();
		private static var m_SwfWindow:SwfSectionLoadWindow;
		private static var m_loader:Loader;
		private static var m_dataLoader:URLLoader;
		private static var m_loadListResource:Array;
		private static var m_loadListXML:Array;
		
		
		// 导入SWF资源
		public static function onImportResource(e:MouseEvent):void
		{
			var file:File;
			
			file = new File();
			try
			{
				file.addEventListener(FileListEvent.SELECT_MULTIPLE,____onSelectMultipleForResource);
				file.browseForOpenMultiple('选择导入资源文件');
			}
			catch(error:Error)
			{
				trace('[Failed] - '+error.message);
			}
		}
		
		
		// 导入SECTION XML
		public static function onImportSectionXML(e:MouseEvent):void
		{
			var file:File;
			
			file = new File();
			try
			{
				file.addEventListener(FileListEvent.SELECT_MULTIPLE,____onSelectMultipleForXML);
				file.browseForOpenMultiple('选择导入资源文件');
			}
			catch(error:Error)
			{
				trace('[Failed] - '+error.message);
			}
		}
		
		
		// 加载SWF片段资源
		public static function onSectionLoadSourceSWF(e:Event):void
		{
			var window:IFlexDisplayObject;
			
			window = m_SwfWindow || (m_SwfWindow = new SwfSectionLoadWindow());
			
			if(!window.parent)
			{
				PopUpManager.addPopUp(window,Despair.root);
				PopUpManager.centerPopUp(window);
				PopUpManager.bringToFront(window);
			}
			else
			{
				PopUpManager.removePopUp(window)
			}
		}
		
		// 加载IMAGE片段资源
		/*public static function onSectionLoadSourceIMAGE(e:Event):void
		{
		var window:IFlexDisplayObject = SwfSectionLoadImpl.instance;
		
		SwfSectionLoadImpl(window).setCurrentState('IMAGE',false);
		if(!window.parent)
		{
		PopUpManager.addPopUp(window,Despair.root);
		PopUpManager.centerPopUp(window);
		PopUpManager.bringToFront(window);
		}
		else
		{
		PopUpManager.removePopUp(window)
		}
		}*/
		
		
		
		private static function ____onSelectMultipleForResource(e:FileListEvent):void
		{
			var list:Array;
			var len:int,i:int;
			var file:File
			var url:String,extension:String;
			
			m_loadListResource = m_loadListResource || (m_loadListResource = []);
			list = e.files;
			len = list.length;
			for(i=0;i<len;i++)
			{
				file = list[i];
				url  = file.url;
				extension = url.substr(url.length - 3,3);
				if(extension != 'swf')
				{
					Alert.show('资源类型 '+extension+') 错误','提示!!');
					m_loadListResource.length = 0;
					return;
				}
				m_loadListResource.push(url);
			}
			
			_serialLoadResource();
		}
		
		
		private static function _serialLoadResource():void
		{
			if(!m_loader)
			{
				m_loader = new Loader();
				m_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,____onLoadedResource);
				var ln:LoaderContext=new LoaderContext();
				ln.applicationDomain=new ApplicationDomain()//.currentDomain;
			}
			
			m_loader.load(new URLRequest(m_loadListResource.pop()),ln);
		}
		
		
		private static function ____onLoadedResource(e:Event):void
		{
			Despair.addApplicationDomainInfo(e.target as LoaderInfo);
			
			m_loader.unload();
			if(!m_loadListResource.length)
			{
				m_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,____onLoadedResource);
				m_loader = null;
			}
			else
			{
				_serialLoadResource();
			}
		}
		
		
		private static function ____onSelectMultipleForXML(e:FileListEvent):void
		{
			var list:Array;
			var len:int,i:int;
			var file:File
			var url:String,extension:String;
			
			m_loadListXML = m_loadListXML || (m_loadListXML = []);
			list = e.files;
			len = list.length;
			for(i=0;i<len;i++)
			{
				file = list[i];
				url  = file.url;
				extension = url.substr(url.length - 3,3);
				if(extension != 'xml')
				{
					Alert.show('资源类型 '+extension+') 错误','提示!!');
					m_loadListXML.length = 0;
					return;
				}
				m_loadListXML.push(url);
			}
			
			_serialLoadXML();
		}
		
		
		private static function _serialLoadXML():void
		{
			if(!m_dataLoader)
			{
				m_dataLoader = new URLLoader();
				m_dataLoader.addEventListener(Event.COMPLETE,____onLoadedXML);
			}
			
			m_dataLoader.load(new URLRequest(m_loadListXML.pop()));
		}
		
		
		private static function ____onLoadedXML(e:Event):void
		{
			var dataLoader:URLLoader;
			var xml:XML;
			
			dataLoader = e.target as URLLoader;
			xml=XML(dataLoader.data) ;
			Despair.registerSections(xml);
			if(!m_loadListXML.length)
			{
				m_dataLoader.removeEventListener(Event.COMPLETE,____onLoadedXML);
				m_dataLoader = null;
			}
			else
			{
				_serialLoadXML();
			}
		}
		
		
	}
}